package system.menus;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.el.ELContext;
import javax.faces.context.FacesContext;
import javax.naming.NamingException;
import system.general.Logged;
import system.general.Persistencia;

/**
 *
 * @author BENIGNO
 */
public class Find {

    Connection con;
    String sql;
    Logged logged;
    private system.general.Persistencia connection = new Persistencia();
    private ResourceBundle rb;

    public Find() {
        ELContext elContext = FacesContext.getCurrentInstance().getELContext();
        logged = (Logged) FacesContext.getCurrentInstance().getApplication().getELResolver().getValue(elContext, null, "logged");
      String idiom = "es";
        if (!logged.getLoggedIdiom().toLowerCase().equals("")) {
            idiom = logged.getLoggedIdiom().toLowerCase();
        }
       rb = ResourceBundle.getBundle("system.menus.label_"+idiom);

    }

    public boolean verificaSubMenu(Bean menu) {
        sql = "select count(*) as total from v_menus  m inner join   v_acceso acc on (acc.modulo=m.id and acc.rol=" + logged.getLoggedRol() + ")  where m.id_padre=" + menu.getId() + " ";
        con = connection.getConn();
        int t = 0;
        try {
            ResultSet rs = connection.executeRsts(sql);
            if (rs.next()) {
                t = rs.getInt("total");
            }
        } catch (SQLException ex) {
            Logger.getLogger(Find.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(Find.class.getName()).log(Level.SEVERE, null, ex);
        }
        return (t > 0);
    }

    public List<Bean> buscaPorMenu(Bean menu) {
        List<Bean> listaMenu = new ArrayList<>();
        sql = "select m.id,m.nombre,m.url,m.id_padre from v_menus  m inner join   v_acceso acc on (acc.modulo=m.id and acc.rol=" + logged.getLoggedRol() + ")  where m.id_padre=" + menu.getId() + " ORDER BY m.id";
        con = connection.getConn();
        try {
            //PreparedStatement pstm = con.prepareStatement(sql);
            //pstm.setInt(1, );
            ResultSet rs = connection.executeRsts(sql);
            while (rs.next()) {
                Bean m = new Bean();
                String menuName = "";
                menu.setId(rs.getInt("id"));
                menuName = this.loadMenu(rs.getString("nombre"));
                  m.setNombre(menuName);
                m.setUrl(rs.getString("url"));
                Bean subMenu = new Bean();
                subMenu = buscaPorId(rs.getInt("id_padre"));
                m.setMenu(subMenu);
                listaMenu.add(m);
            }
            con.close();
        } catch (SQLException ex) {
            Logger.getLogger(Find.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NamingException ex) {
            Logger.getLogger(Find.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(Find.class.getName()).log(Level.SEVERE, null, ex);
        }
        return listaMenu;
    }

    public Bean buscaPorId(int id) throws SQLException, NamingException, Exception {
        sql = "select  m.id,m.nombre,m.url,m.id_padre from v_menus  m inner join   v_acceso acc on (acc.modulo=m.id and acc.rol=" + logged.getLoggedRol() + ")        where m.id=" + id + " ORDER BY m.id";
        con = connection.getConn();
        Bean menu = new Bean();
        try {
            //PreparedStatement pstm = con.prepareStatement(sql);
            //pstm.setInt(1, id);
            ResultSet rs = connection.executeRsts(sql);
            if (rs.next()) {
                String menuName = "";
                menu.setId(rs.getInt("id"));
                menuName = this.loadMenu(rs.getString("nombre"));
                menu.setNombre(menuName);
                menu.setUrl(rs.getString("url"));
                Bean subMenu = new Bean();
                subMenu = buscaPorId(rs.getInt("id_padre"));
                menu.setMenu(subMenu);
            }
            con.close();
        } catch (SQLException ex) {
            Logger.getLogger(Find.class.getName()).log(Level.SEVERE, null, ex);
        }
        return menu;
    }

    public List<Bean> listaTodos() {
        List<Bean> listaMenu = new ArrayList<>();
        sql = "select m.id,m.nombre,m.url,m.id_padre from v_menus  m inner join   v_acceso acc on (acc.modulo=m.id and acc.rol=" + logged.getLoggedRol() + ")   ORDER BY m.id";
        con = connection.getConn();
        try {
            ResultSet rs = connection.executeRsts(sql);
            while (rs.next()) {
                Bean menu = new Bean();
                String menuName = "";
                menu.setId(rs.getInt("id"));
                menuName = this.loadMenu(rs.getString("nombre"));
                menu.setNombre(menuName);
                menu.setUrl(rs.getString("url"));
                Bean subMenu = new Bean();
                subMenu = buscaPorId(rs.getInt("id_padre"));
                menu.setMenu(subMenu);
                listaMenu.add(menu);
            }
            con.close();
        } catch (SQLException ex) {
            Logger.getLogger(Find.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NamingException ex) {
            Logger.getLogger(Find.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(Find.class.getName()).log(Level.SEVERE, null, ex);
        }
        return listaMenu;
    }

    private String loadMenu(String id) {
        return rb.getString(id);
    }
}